import modelExtend from 'dva-model-extend'
import { query, detail, approve, deny, editDisplayName } from './services/salesDistributorRegistryApplyService'
import { allList } from  './services/product'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router';
import {registryTypes} from 'utils/enumConfig'

export default modelExtend(lwjPageModel, {
  namespace: "salesDistributorRegistryApply",
  state: {
    currentItem: {},
    pageOptions: {
      sort: 'createTime',
      order: 'desc'
    },
    detailVisible: false,
    fastVisible: false,
    auditVisible: false,
    editVisible: false,
    auditItem: {},
    queryParam: {},
    currentType: "SALES",
    commodityStoreList: [],
    currentOption:"",
    typeObj: registryTypes
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === "/wx/salesDistributorRegistryApply/salesDistributorRegistryApplylist") {
          dispatch({ type: 'query', payload: {} })
          dispatch({ type: 'allList', payload: {} })
        }
      })
    }

  },

  effects: {
    * allList({
      payload
    }, { select, call, put }) {
      const data = yield call(allList, payload);
      yield put({
        type: 'setCommodityStoreList',
        payload: data.result
      })
    },
    * deny({
      params
    }, { select, call, put }) {
      yield call(deny, params);
      yield put({
        type: 'detail',
        payload: params.id
      })
      yield put({
        type: 'toggleAuditVisible',
        auditVisible: false
      })
      let queryParam = yield select(({ salesDistributorRegistryApply }) => salesDistributorRegistryApply.queryParam);
      yield put({
        type: 'query',
        payload: queryParam
      })
    },
    * approve({
      params
    }, { select, call, put }) {
      let data = yield call(approve, params);
      yield put({
        type: 'detail',
        payload: params.id
      })
      let queryParam = yield select(({ salesDistributorRegistryApply }) => salesDistributorRegistryApply.queryParam);
      yield put({
        type: 'query',
        payload: queryParam
      })
    },
    * detail({
      payload
    }, { select, call, put }) {
      const data = yield call(detail, payload);
      data.enterPicArr = (data.enterPic ? data.enterPic.split(';') : null);
      yield put({
        type: 'setCurrentItem',
        payload: data
      })
    },
    * query({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ salesDistributorRegistryApply }) => salesDistributorRegistryApply.pageOptions);
      const queryParam = yield select(({ salesDistributorRegistryApply }) => salesDistributorRegistryApply.queryParam);
      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam,

        }
      };
      const data = yield call(query, queryPayload);
      yield put({
        type: 'freshState',
        payload: {

        }
      })
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },
    * editDisplayName({currentItem}, {select, call, put}) {
      let params = {
        id: currentItem.id,
        displayName: currentItem.copyDisplayName
      };
      let data = yield call(editDisplayName, params);
      yield put({
        type: 'toggleEditVisible',
        editVisible: false
      });
      yield put({
        type: 'detail',
        payload: currentItem.id
      });
      let queryParam = yield select(({ salesDistributorRegistryApply }) => salesDistributorRegistryApply.queryParam);
      yield put({
        type: 'query',
        payload: queryParam
      })
    }
  },
  reducers: {
    onSelectChange(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    setCurrentTab(state, { payload }) {
      return {
        ...state,
        currentType: payload
      }
    },
    freshState(state, { payload }) {
      return {
        ...state,
        selectRows: [],
        selectedRowKeys: []
      }
    },
    setCurrentItem(state, { payload }) {
      return {
        ...state,
        currentItem: payload
      }
    },
    setDetailVisible(state, { payload }) {
      return {
        ...state,
        detailVisible: payload
      }
    },
    closeAllModal(state, { payload }) {
      return {
        ...state,
        auditVisible: false,
        detailVisible: false
      }
    },
    toggleAuditVisible(state, { auditVisible, auditItem }) {
      return {
        ...state,
        auditVisible: auditVisible,
        auditItem: (auditItem ? auditItem : {})
      }
    },
    toggleEditVisible(state, { editVisible}) {
      return {
        ...state,
        editVisible: editVisible
      }
    },
    setAuditReason(state, { auditReason }) {
      let auditItem = state.auditItem;
      auditItem.auditReason = auditReason;
      return {
        ...state,
        auditItem: auditItem
      }
    },
    setFastVisible(state, { payload }) {
      return {
        ...state,
        fastVisible: payload
      }
    },
    setCommodityStoreList(state, { payload }) {
      return {
        ...state,
        commodityStoreList: payload
      }
    },
    setCurrentOption(state, { payload }) {
      return {
        ...state,
        currentOption: payload
      }
    },
  }
})

